package phapp.springSecurity;

import org.springframework.security.core.*;
import org.springframework.security.core.context.*;


public class LogonUser
{

   public static SpringAuthUser getLogonUser()
   {
      Object objUser = null;
      SpringAuthUser authUser = null;
      
      if (SecurityContextHolder.getContext().getAuthentication() != null)
      {
         objUser = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
         if (objUser instanceof SpringAuthUser)
         {
            authUser = (SpringAuthUser) objUser;
         }
         else
         {
            //System.out.println("getAuthentication - " + objUser.toString() + "\n\n\n");
            authUser = null;
         }
      }
      else
      {
         authUser = null;
         //System.out.println("getAuthentication - null\n\n\n");
      }

      return authUser;
   }

   public static String getUserId()
   {
      String userId = null;
      SpringAuthUser authUser = null;
      
      authUser = LogonUser.getLogonUser();
      if (null != authUser)
      {
         userId = authUser.getUserId();
      }
      
      return userId;
   }

   public static String getUserAccount()
   {
      String userAccount = "";
      SpringAuthUser authUser = null;
      
      authUser = LogonUser.getLogonUser();
      if (null != authUser)
      {
         userAccount = authUser.getUsername();
      }
      
      return userAccount;
   }
   
   public static String getUserName()
   {
      String userName = "";
      SpringAuthUser authUser = null;
      
      authUser = LogonUser.getLogonUser();
      if (null != authUser)
      {
         userName = authUser.getUserName();
      }
      
      return userName;
   }

}
